<?php
class AuthenticationController 
	extends Zend_Controller_Action 
{

	public function loginAction(){
	
	    if ( ! $this->getRequest()->isXmlHttpRequest()){
	    
	        $session = Zend_Registry::get('session');
	        $session->loginTrigger = true;
	        return $this->getHelper('Redirector')->gotoUrlAndExit('/');
	    }
	    
	    
		$form = new Web_Form_Entrar;
		$form->setAction(		
			$this->getHelper('Url')->url(array(), 'process-login')
		);
		
		if($this->_request->isXmlHttpRequest()){
			 $this->_helper->layout->disableLayout();
		}
		
		$this->view->form = $form;
		$this->render('entrar');
	} 
	
	public function authenticateAction(){
	
		$session = Zend_Registry::get('session');
        $request = $this->getRequest();

        $result = new stdClass();
        
        if( isset($session->comeBack) ){
        	$returnTo = $session->comeBack;
        	unset($session->comeBack);
        } else {
        	$returnTo = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : null;
        }
        
        $email 	= $request->getParam('email');
        $password	= $request->getParam('password');

        Zend_Session::rememberMe(86400);// 1 dia
        
        try{
        	
        	$mapper	= Vs_Factory::mapper( 'User' );
			$user= $mapper->authenticate( $email, $password);
        	
        }
        catch (Vs_Exception $e){
        	$result = array(
				'success' => false,
				'message'=>	$e->getMessage()		
			);			
			$this->_helper->Json($result);
        
        }catch (Zend_Exception $e){
			
			$result = array(
				'success' => false,
				'message'=>	$e->getMessage()	
			);			
			$this->_helper->Json($result);
			
			$this->getHelper('MyFlashMessenger')->addError( $$e->getMessage() );
			
			$this->_helper->Json($result);
		}         
             
        $session->userId = $user->userId;
        
		$this->_helper->Json( array('success' => true, 'comeback' => $returnTo) );
	}
	
	public function logoutAction(){
		
		$session = Zend_Registry::get('session');
		
		if (isset($session->userId)){
			unset( $session->userId );
			Zend_Session::destroy(false);
		}
		
		$this->_redirect('/');	
	}
	
	
	
	public function changePasswordAction()
	{
		$email 	= $this->getRequest()->getParam('email');
		$codigo	= $this->getRequest()->getParam('codigo');	
		
		$form = new Web_Form_Clave;
		$form->setAction(		
			$this->getHelper('Url')->url(array('email'=>$email, 'codigo'=>$codigo), 'reset')
		);		
		
		try{
			
			$mapper	= Delivery_Model_Factory::mapper( 'Clientes' );		
			$cliente = $mapper->getByEmail ($email);
		
			if ( $this->getRequest()->isPost() ){
				
				$data = $this->getRequest()->getPost();
				if ($form->isValid($data)){
						
					$cliente->cambiarClave( $this->getRequest()->getParam('clave'), $codigo );
					$mapper->save( $cliente );
					$this->getHelper('MyFlashMessenger')->addSuccess( $this->_helper->Translate('passwordHasBeenChanged') );				
					return $this->getHelper('Redirector')->gotoRoute(array(), 'login');	
					
				} else {
					$form->populate($data);				
				}

			} 
		
		} catch ( Delivery_Model_Exception $e ){
			$this->getHelper('Log')->exception( $e );
			$this->getHelper('MyFlashMessenger')->addError( $this->_helper->Translate($e->getMessage()) );
			throw $e;
		} 
		
		$this->view->form = $form;
		$this->render('cambiar-clave');
	}
	
}